# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
from scrapy.conf import settings
import time
class WeiboPipeline(object):
    def open_spider(self,spider):
        SQL=settings['MYSQL']
        self.coon=pymysql.connect(SQL['host'],SQL['user'],SQL['pwd'],SQL['db'],charset='utf8')
        self.cursor=self.coon.cursor()
    def process_item(self, item, spider):
        print(time.ctime())
        try:
            sql,data=item.save_to_mysql()
            self.cursor.execute(sql,data)
            self.coon.commit()
            print('写入成功OK')
        except Exception as e:
            print('写入失败,原因为%s'%e)
        return item
    def close_spider(self,spider):
        print(time.ctime())
        self.cursor.close()
        self.coon.close()
